package com.aica.handler;


import com.aica.constant.ResultConstant;
import com.aica.constant.ValidationNameConstant;
import com.aica.vo.ResultVo;
import com.auth0.jwt.exceptions.JWTVerificationException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.Objects;

/**
 * 全局捕获异常
 * @author yly
 * @date 2021/2/10 11:17
 */
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler  {


    /**
     * 如果异常产生于进入controller前，那么方法不可达
     * jwt校验失败，返回密匙过期
     */
    @Deprecated
    @ExceptionHandler(JWTVerificationException.class)
    public ResponseEntity<Object> handleJwtVerificationException(JWTVerificationException ex){
        return new ResponseEntity<>(
                ResultConstant.TOKEN_EXPIRE,
                HttpStatus.OK
        );
    }

    /**
     * 捕获全局500异常，统一格式返回
     */
//    @ExceptionHandler(Exception.class)
//    public ResponseEntity<Object> handleException(Exception ex){
//        return new ResponseEntity<>(
//                ResultConstant.SYSTEM_ERROR,
//                HttpStatus.INTERNAL_SERVER_ERROR
//        );
//    }

    /**
     * 缺失请求参数
     */
    @Override
    protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        System.out.println(ex.getParameterName());
        return new ResponseEntity<>(
                new ResultVo<>().emptyParamError(ex.getParameterName()),
                HttpStatus.BAD_REQUEST
        );
    }

    /**
     * 缺失请求参数
     */
    @Override
    protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        System.out.println(ex.getVariableName());
        return new ResponseEntity<>(
                new ResultVo<>().emptyParamError(ex.getVariableName()),
                HttpStatus.BAD_REQUEST
        );
    }

    /**
     * 参数格式不正确
     */
    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

        //空串处理
        if (ValidationNameConstant.NOT_EMPTY.equals(Objects.requireNonNull(ex.getBindingResult().getFieldError()).getCode())) {
            return new ResponseEntity<>(new ResultVo<>().emptyParamError(ex.getBindingResult().getFieldError().getField()), HttpStatus.BAD_REQUEST);
        }
        //邮箱格式处理
        if (ValidationNameConstant.EMAIL.equals(ex.getBindingResult().getFieldError().getCode())) {
            return new ResponseEntity<>(ResultConstant.EMAIL_FORMAT_ERROR, HttpStatus.BAD_REQUEST);
        }

        //其他格式校验处理
        if (ValidationNameConstant.PATTERN.equals(ex.getBindingResult().getFieldError().getCode())) {
            switch (ex.getBindingResult().getFieldError().getField()){
                //用户名
                case ValidationNameConstant.PATTERN_USER_NAME_1:
                case ValidationNameConstant.PATTERN_USER_NAME_2:
                    return new ResponseEntity<>(ResultConstant.USER_NAME_FORMAT_ERROR,HttpStatus.BAD_REQUEST);
                //密码
                case ValidationNameConstant.PATTERN_PASSWORD:
                case ValidationNameConstant.PATTERN_OLD_PASSWORD:
                case ValidationNameConstant.PATTERN_NEW_PASSWORD:
                    return new ResponseEntity<>(ResultConstant.PASSWORD_FORMAT_ERROR,HttpStatus.BAD_REQUEST);
                //电话
                case ValidationNameConstant.PATTERN_PHONE_NUMBER:
                    return new ResponseEntity<>(ResultConstant.PHONE_NUMBER_FORMAT_ERROR,HttpStatus.BAD_REQUEST);
                default:
            }
        }
        //系统故障，用于提醒开发者请求类中有使用注解校验格式的字段的名字不在预测之内，即字段名不是本项目标准规定的名字
        return new ResponseEntity<>(ResultConstant.SYSTEM_FIELD_ERROR, HttpStatus.INTERNAL_SERVER_ERROR);
    }


}
